package com.jokerpan.project.commons;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author jokerpan
 */
public class ResultBuilder implements Serializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResultBuilder.class);
    private static final String SUCCESS = "success";
    private static final String FAILED = "操作失败!";

    private ResultBuilder() {

    }

    public static ListResult<Object> buildListSuccess() {
        ListResult<Object> listResult = new ListResult<>();
        try {
            listResult.setData(new Object[]{});
            listResult.setStatus(HttpStatus.OK.value());
            listResult.setMessage(SUCCESS);
        } catch (Exception e) {
            ResultBuilder.buildListWarn(listResult, e);
        }
        return listResult;
    }

    public static ListResult<Object> buildListSuccess(String data) {
        ListResult<Object> listResult = new ListResult<>();
        try {
            listResult.setData(data);
            listResult.setStatus(HttpStatus.OK.value());
            listResult.setMessage(SUCCESS);
        } catch (Exception e) {
            ResultBuilder.buildListWarn(listResult, e);
        }
        return listResult;
    }

    public static ListResult<Object> buildListSuccess(Map<String, Object> data) {
        ListResult<Object> listResult = new ListResult<>();
        try {
            listResult.setData(data);
            listResult.setStatus(HttpStatus.OK.value());
            listResult.setMessage(SUCCESS);
        } catch (Exception e) {
            ResultBuilder.buildListWarn(listResult, e);
        }
        return listResult;
    }

    public static <T>ListResult<Object> buildListSuccess(T data) {
        ListResult<Object> listResult = new ListResult<>();
        try {
            listResult.setData(data);
            listResult.setStatus(HttpStatus.OK.value());
            listResult.setMessage(SUCCESS);
        } catch (Exception e) {
            ResultBuilder.buildListWarn(listResult, e);
        }
        return listResult;
    }

    public static ListResult<Object> buildListSuccess(List<?> data) {
        ListResult<Object> listResult = new ListResult<>();
        try {
            listResult.setData(data);
            listResult.setStatus(HttpStatus.OK.value());
            listResult.setMessage(SUCCESS);
        } catch (Exception e) {
            ResultBuilder.buildListWarn(listResult, e);
        }
        return listResult;
    }

    public static ListResult<Object> buildListFail(Exception e) {
        ListResult<Object> listResult = new ListResult<>();
        listResult.setData(Collections.emptyMap());
        listResult.setStatus(HttpStatus.BAD_REQUEST.value());
        listResult.setMessage(e.getMessage());

        return listResult;
    }

    public static ListResult<Object> buildListFail(String msg) {
        ListResult<Object> listResult = new ListResult<>();
        listResult.setData(Collections.emptyMap());
        listResult.setStatus(HttpStatus.BAD_REQUEST.value());
        listResult.setMessage(msg);

        return listResult;
    }
    public static ListResult<Object> buildListFail(Exception e,HttpStatus httpStatus) {
        return buildListFail(e.getMessage(),httpStatus);
    }

    public static ListResult<Object> buildListFail(String msg,HttpStatus httpStatus) {
        ListResult<Object> listResult = new ListResult<>();
        listResult.setData(Collections.emptyMap());
        listResult.setStatus(httpStatus.value());
        listResult.setMessage(msg);
        return listResult;
    }

    public static void buildListWarn(ListResult<Object> result, Exception e) {
        result.setData(Collections.emptyMap());
        result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(e.getMessage());
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn(e.toString());
        }
    }

    public static ActionResult buildActionSuccess() {
        ActionResult actionResult = new ActionResult();
        try {
            actionResult.setStatus(HttpStatus.OK.value());
            actionResult.setMessage(SUCCESS);
        } catch (Exception e) {
            ResultBuilder.buildActionWarn(actionResult, e);
        }
        return actionResult;
    }

    public static ActionResult buildActionFailed() {
        ActionResult actionResult = new ActionResult();
        try {
            actionResult.setStatus(HttpStatus.BAD_REQUEST.value());
            actionResult.setMessage(FAILED);
        } catch (Exception e) {
            ResultBuilder.buildActionWarn(actionResult, e);
        }
        return actionResult;
    }

    public static ActionResult buildActionFailed(Object msg) {
        ActionResult actionResult = new ActionResult();
        try {
            actionResult.setStatus(HttpStatus.BAD_REQUEST.value());
            actionResult.setMessage(msg.toString());
        } catch (Exception e) {
            ResultBuilder.buildActionWarn(actionResult, e);
        }
        return actionResult;
    }

    public static void buildActionWarn(ActionResult result, Exception e) {
        result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(e.getMessage());
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn(e.toString());
        }
    }
}
